package com.shuyou.lib;

import java.util.List;

import com.shuyou.R;
import com.shuyou.lib.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BookOwnersListViewAdapter extends ArrayAdapter<BookOwnersListView> {

	    private ListView listView;
	    private AsyncImageLoader asyncImageLoader;

	    public BookOwnersListViewAdapter(Activity activity, List<BookOwnersListView> bookOwnersListView, ListView listView) {
	        super(activity, 0, bookOwnersListView);
	        this.listView = listView;
	        asyncImageLoader = new AsyncImageLoader();
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        Activity activity = (Activity) getContext();

	        // Inflate the views from XML
	        View rowView = convertView;
	        BookOwnersViewCache viewCache;
	        if (rowView == null) {
	            LayoutInflater inflater = activity.getLayoutInflater();
	            rowView = inflater.inflate(R.layout.shuyou_listview_bookowners, null);
	            viewCache = new BookOwnersViewCache(rowView);
	            rowView.setTag(viewCache);
	        } else {
	            viewCache = (BookOwnersViewCache) rowView.getTag();
	        }
	        BookOwnersListView bookOwnersView = getItem(position);

	        // Load the image and set it on the ImageView
	        String imageUrl = bookOwnersView.getImageUrl();
	        ImageView imageView = viewCache.getLogoImageView();
	        imageView.setTag(imageUrl);
	        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
	            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
	                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
	                if (imageViewByTag != null) {
	                    imageViewByTag.setImageDrawable(imageDrawable);
	                }
	            }
	        });
			if (cachedImage == null) {
				imageView.setImageResource(R.drawable.ic_launcher);
			}else{
				imageView.setImageDrawable(cachedImage);
			}
			
			viewCache.getPhoneNumPosition().setVisibility(View.VISIBLE);
			viewCache.getQqPosition().setVisibility(View.VISIBLE);
			viewCache.getWechatPosition().setVisibility(View.VISIBLE);
	        // Set the text on the TextView
	        TextView textNickName = viewCache.getNickNameTextView();
	        textNickName.setText(bookOwnersView.getNickName()+"");
	        
	        String phoneNum = bookOwnersView.getPhoneNum();
	        if(phoneNum == null) {
	        	viewCache.getPhoneNumPosition().setVisibility(View.GONE);
	        } else if (phoneNum.equals("")) {
	        	viewCache.getPhoneNumPosition().setVisibility(View.GONE);
	        } else {
	        	TextView textPhoneNum = viewCache.getPhoneNumTextView();
		        textPhoneNum.setText(phoneNum);
	        }
	        
	        String qq = bookOwnersView.getQq();
	        if(qq == null) {
	        	viewCache.getQqPosition().setVisibility(View.GONE);
	        } else if (qq.equals("")){
	        	viewCache.getQqPosition().setVisibility(View.GONE);
	        } else {
		        TextView textQq = viewCache.getQQTextView();
		        textQq.setText(qq);
	        }
	        
	        String wechat = bookOwnersView.getWechat();
	        if(wechat == null) {
	        	viewCache.getWechatPosition().setVisibility(View.GONE);
	        } else if(wechat.equals("")) {
	        	viewCache.getWechatPosition().setVisibility(View.GONE);
	        } else {
		        TextView textWechat = viewCache.getWechatTextView();
		        textWechat.setText(wechat);
	        }
	        
	        TextView textComment = viewCache.getCommentTextView();
	        textComment.setText(bookOwnersView.getComment());

	        return rowView;
	    }

}
